<template>
    <el-form label-width="100px" :rules="rules" ref="formRef" :model="form">
        <el-form-item label="问题题目" prop="questionTitle">
            <el-input v-model="form.questionTitle" aria-placeholder="请输入问题题目"></el-input>
        </el-form-item>
        <el-form-item label="问题内容" prop="questionContent">
            <el-input v-model="form.questionContent" type="textarea" :rows="4" aria-placeholder="请输入问题内容"></el-input>
        </el-form-item>
        <el-form-item label="用户id" prop="systemUserId">
            <el-input-number v-model="form.systemUserId" :min="1" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="resetForm">重置</el-button>
        </el-form-item>
    </el-form>
</template>
<script >
import {ref} from 'vue';
import {ElMessage} from 'element-plus';
import axios from 'axios';

export default{
    setup(){
        const form = ref({
            questionTitle:'',
            questionContent:'',
            systemUserId:''
        });

        const rules = {
            questionTitle:[
                {required: true,message:'请输入问题题目' , trigger:'blur'}
            ],
            questionContent:[
                {required: true,message:'请输入问题内容' , trigger:'blur'}
            ], 
            systemUserId:[
                {required: true,message:'请输入用户ID' , trigger:'blur'}
            ]
        };

        const formRef = ref(null);


        const submitForm = () =>{
            formRef.value.validate(async (valid) =>{
                
                if(valid){
                    await axios.post("api/question/add",form.value)
                    ElMessage.success('提交成功!');
                    console.log('表单数据：',form.value);
                    
                }else{
                    ElMessage.error('请检查输入内容')
                }
            });
        };


        const resetForm = () => {
            formRef.value.resetFields();
        };

        return {
            form,
            rules,
            formRef,
            submitForm,
            resetForm
        };
    }
};
</script>
<style lang="less" scoped>
.el-form{
    max-width: 500px;
    margin: 0 auto;
}
</style>
